package IT.DataAccess;

import IT.Entities.Sucursal;
import java.util.List;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatSucursal implements IDatSucursal{
    
    private Sucursal oSucursal;
    private List<Sucursal> oLista;
    private BDConexion oBase;
    
    private Connection oConexion;
    private CallableStatement oCst;
    private ResultSet oRst;
    
    public List<Sucursal> Listar() {
        oLista = new ArrayList<Sucursal>();
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_SucursalListar()}");
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oLista.add(new Sucursal(oRst.getInt("cod_suc_in"), 
                        oRst.getString("nom_suc_vc"),
                        oRst.getInt("est_suc_in"),oRst.getString("Estado")));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
        return oLista;
    }

    public Sucursal Ind(int nCodigo) {
        oSucursal=new Sucursal();
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_SucursalInd(?)}");
            oCst.setInt(1, nCodigo);
            oRst = oCst.executeQuery();

            while (oRst.next()) {
                oSucursal=new Sucursal(oRst.getInt("cod_suc_in"), 
                        oRst.getString("nom_suc_vc"),
                        oRst.getInt("est_suc_in"),oRst.getString("Estado"));
            }
            oRst.close();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatArea.class.getName()).log(Level.SEVERE, null, ex);
        }
        return oSucursal;
    }

    public void Insertar(Sucursal ent) {
         try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_SucursalInsertar(?,?)}");              
            oCst.setString(1, ent.getNom_suc_vc());
            oCst.setInt(2, ent.getEst_suc_in());
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatGrupoMaterial.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Editar(Sucursal ent) {
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_SucursalEditar(?,?,?)}");
            
            oCst.setInt(1, ent.getCod_suc_in());
            oCst.setString(2, ent.getNom_suc_vc());
            oCst.setInt(3, ent.getEst_suc_in());
            
            oCst.execute();
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatGrupoMaterial.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int Eliminar(int nCodigo) {
         int nResultado=0;
        
          try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_SucursalEliminar(?,?)}");
            oCst.setInt(1, nCodigo);
            oCst.registerOutParameter(2, java.sql.Types.INTEGER);
            oCst.execute();
            nResultado=oCst.getInt(2);
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatGrupoMaterial.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return nResultado;
    }

    public int Valida(int nCodigo, String cSucursal) {
        int nValida=0;        
        try {
            oBase = new BDConexion();
            oConexion = oBase.getDataSource().getConnection();
            oBase = null;
            oCst = oConexion.prepareCall("{call usp_SucursalValida(?,?,?)}");              
            oCst.setInt(1, nCodigo);
            oCst.setString(2, cSucursal);
            oCst.registerOutParameter(3, java.sql.Types.INTEGER);
            oCst.execute();
            nValida=oCst.getInt(3);
            oCst.close();
            oConexion.close();
        } catch (SQLException ex) {
            Logger.getLogger(DatGrupoMaterial.class.getName()).log(Level.SEVERE, null, ex);
        }
        return nValida;
    }

}
